package com.qf.service.impl;

import com.qf.dao.UserMapper;
import com.qf.pojo.TbUser;
import com.qf.service.UserService;
import com.qf.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * Created by 54110 on 2021/4/16.
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseResult login(TbUser tbUser, HttpSession session) {
        //声明 返回

        BaseResult baseResult = new BaseResult();
        //进行验证码的认证
        Object captcha = session.getAttribute("captcha");
        if (!tbUser.getCode().equals(captcha)){
            baseResult.setCode(20003);
            baseResult.setMessage("验证码输入错误！请重新输入");
            return baseResult;
        }
        String userName = tbUser.getUserName();
        TbUser user =  userMapper.findByUserName(userName);
        if (user==null){
            baseResult.setCode(20001);
            baseResult.setMessage("没有该用户，请先注册");
            return baseResult;
        }
        if (!user.getPassword().equals(tbUser.getPassword())){
            baseResult.setCode(20001);
            baseResult.setMessage("密码输入错误");
            return baseResult;
        }
        //放置到session中
        session.setAttribute("user",tbUser);
        baseResult.setCode(200);
        baseResult.setMessage("登录成功！");
        return baseResult;
    }
}
